<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Model_Member
 *
 * @author bxsp
 */
class Member_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->library('encrypt');
    }

    /**
     * 创建超级管理员
     * @param type $username
     * @param type $password
     * @return type
     */
    public function creatAdmin($username, $password) {
        $encrypt_password = $this->encrypt->encode($password);
        $data = array(
            'username' => $username,
            'password' => $encrypt_password,
            'role' => 1,
        );
        $ret = $this->db->insert('ci_member', $data);
        return $ret;
    }

    /**
     * 检查账户密码
     * 设置session
     * @param type $username
     * @param type $password
     * @return boolean
     */
    public function checkLogin($username, $password) {
        $sql = 'SELECT * FROM ci_member WHERE username = ?';
        $ret = $this->db->query($sql, array($username))->row();
        $encrypt_password = $this->encrypt->decode($ret->password);
        if ($encrypt_password === $password && $ret->role === '1') {
            $this->session->set_userdata(array(
                'uid' => $ret->uid,
                'name' => $ret->username,
                'role' => $ret->role,
            ));
            return TRUE;
        } else {
            return FALSE;
        }
    }

}
